<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <style>
        .center{
            width: 1200px;
            margin: 0 auto;
        }
        #footer_center p{
            margin: 0;
        }
        #footer_center h3{
            color: white;
        }
    </style>
</head>
<body>
<div id="app">
    <el-container>
        <!--头,引入my-header.js-->
       <my-header></my-header>
        <!--中间,主要内容部分-->
        <el-main class="center" style="margin-top: 30px">
            <el-row gutter="20">
                <el-col span="12">
                    <el-card>
                        <img src="http://rwoyy67xx.hn-bkt.clouddn.com/img/reg.gif" width="100%">
                    </el-card>
                </el-col>
                <el-col span="12">
                    <!--注册表单开始-->
                    <el-form label-width="80px" :model="user" :rules="rules" ref="userForm">
                        <el-form-item>
                            <h1 style="float: left;font-size: 28px">立即注册</h1>
                            <a style="float: right;
                            text-decoration: none;color: #409EFF;
                            position: relative;top: 20px"  href="login.html">
                                已有账号?现在登录
                            </a>
                        </el-form-item>
                        <el-form-item label="用户名" prop="username">
                            <el-input type="text" placeholder="请输入用户名" v-model="user.username"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input type="text" placeholder="请输入密码" v-model="user.password"></el-input>
                        </el-form-item>
                        <el-form-item label="昵称" prop="nickname">
                            <el-input type="text" placeholder="请输入昵称" v-model="user.nickname"></el-input>
                        </el-form-item>
                        <el-form-item label="邮箱" prop="email">
                            <el-input type="text" placeholder="请输入邮箱" v-model="user.email"></el-input>
                        </el-form-item>
                        <el-form-item label="电话" prop="mobile">
                            <el-input type="text" placeholder="请输入电话" v-model="user.mobile"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="submitForm()">注册</el-button>
                        </el-form-item>
                    </el-form>
                    <!--注册表单结束-->
                </el-col>
            </el-row>

        </el-main>
        <!--脚-->
        <el-footer style="height: 350px;background-color: darkgrey;padding:50px 0">
            <my-footer></my-footer>
        </el-footer>
    </el-container>
</div>
</body>
<!--import axios-->
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<!--引入自己的js文件-->
<script src="js/my-header.js"></script>
<script src="js/my-footer.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data:  {
          user:{username:"",password:"",nickname:"",email: "",mobile:""},
            rules: {
                username: [
                    {required: true, message: '请输入用户名', trigger: 'blur'},
                    {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
                ],
                password: [
                    {required: true, message: '请输入密码', trigger: 'blur'},
                    {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}
                ],
                nickname: [
                    {required: true, message: '请输入昵称', trigger: 'blur'},
                    {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
                ],
                email: [
                    {required: true, message: '请输入邮箱', trigger: 'blur'},
                    {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur'}
                ],
                mobile: [
                    {required: true, message: '请输入电话', trigger: 'blur'},
                    {pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur'}
                ]
            }
        },
        methods: {
            //表单数据验证
            submitForm() {
                this.$refs['userForm'].validate((valid) => {
                    if (valid) {
                        this.reg();
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            reg(){
                //发出异步请求
                axios.post("/v1/users/reg",v.user).then(function (response){
                    if(response.data.code==2001){
                        alert("注册成功");
                        //    注册成功 返回首页
                        location.href="/";
                    }else{
                        alert("用户名已存在");
                    }
                })
            }
        }
    })
</script>
</html>